package com.service.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.service.dao.model.NotifyMessage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface NotifyMessageMapper extends BaseMapper<NotifyMessage> {

    /**
     * 统计用户全部未读消息数
     * @param memberId
     * @return
     */
    @Select("select count(1) from hm_notify_message where member_id = #{memberId} and is_read = 0")
    Integer countNotReadByMemberId(@Param("memberId") Integer memberId);

    /**
     * 根据消息类型统计用户未读消息数
     * @param memberId
     * @return
     */
    @Select("select count(1) from hm_notify_message where member_id = #{memberId} and message_type = #{messageType} and is_read = 0")
    Integer countNotReadByMemberIdAndType(@Param("memberId") Integer memberId,
                                          @Param("messageType") Integer messageType);

    /**
     * 当前用户所有消息全部已读
     * @param memberId
     * @return
     */
    @Update("update hm_notify_message set is_read = 1 where member_id = #{memberId}")
    int readAllByMemberId(@Param("memberId")Integer memberId);

    /**
     * 当前用户某种消息全部已读
     * @param memberId
     * @param messageType
     * @return
     */
    @Update("update hm_notify_message set is_read = 1 where member_id = #{memberId} and message_type = #{messageType}")
    int readAllByMemberIdAndType(@Param("memberId")Integer memberId,
                                 @Param("messageType") Integer messageType);

    /**
     * 具体某个用户的某条消息已读
     * @param memberId
     * @param messageType
     * @param targetId
     * @return
     */
    @Update("update hm_notify_message set is_read = 1 where member_id = #{memberId} and message_type = #{messageType} and target_id = #{targetId}")
    int readByMemberAndTypeAndTarget(@Param("memberId")Integer memberId,
                                     @Param("messageType") Integer messageType,
                                     @Param("targetId") Integer targetId);
}
